package com.merry.jedis.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.Tuple;

import java.util.List;
import java.util.Map;

/**
 * 删除bigkey的demo
 * bigkey的删除，除了list使用的是ltrim指令一次移除一点，其他的都是使用scan指令，一点点删除。
 */
public class BigKeyDemo {

    //删除hash类型的bigkey
    public void delBigHash(String host,int port,String password,String bigHashKey){
        Jedis jedis = new Jedis(host,port);
        if(password != null && !"".equals(password)){
            jedis.auth(password);
        }
        ScanParams scanParams=new ScanParams().count(100);
        String cursor = "0";
        do{
            ScanResult<Map.Entry<String, String>> hscan = jedis.hscan(bigHashKey, cursor, scanParams);
            List<Map.Entry<String, String>> result = hscan.getResult();
            if(result != null && !result.isEmpty()){
                for (Map.Entry<String,String> entry:result){
                    jedis.hdel(bigHashKey,entry.getKey());
                }
            }
            cursor=hscan.getStringCursor();
        }while (!"0".equals(cursor));

        //最后删除key
        jedis.del(bigHashKey);
    }

    //删除list类型的bigkey
    public void delBigList(String host,int port,String password,String bigListKey){
        Jedis jedis = new Jedis(host,port);
        if(password != null && !"".equals(password)){
            jedis.auth(password);
        }
        long llen=jedis.llen(bigListKey);
        int counter=0;
        int left = 100;
        while(counter<llen){
            //每次从左边删除100个。
            jedis.ltrim(bigListKey,left,llen);
            counter += left;
        }
        //最后删除key
        jedis.del(bigListKey);
    }

    //删除set类型的bigkey
    public void delBigSet(String host,int port,String password,String bigSetKey){
        Jedis jedis = new Jedis(host,port);
        if(password != null && !"".equals(password)){
            jedis.auth(password);
        }
        ScanParams scanParams=new ScanParams().count(100);
        String cursor = "0";
        do{
            ScanResult<String> sscan = jedis.sscan(bigSetKey, cursor, scanParams);
            List<String> result = sscan.getResult();
            if(result != null && !result.isEmpty()){
                for (String item : result){
                    jedis.hdel(bigSetKey,item);
                }
            }
            cursor=sscan.getStringCursor();
        }while (!"0".equals(cursor));

        //最后删除key
        jedis.del(bigSetKey);
    }

    //删除zset类型的bigkey
    public void delBigZSet(String host,int port,String password,String bigZSetKey){
        Jedis jedis = new Jedis(host,port);
        if(password != null && !"".equals(password)){
            jedis.auth(password);
        }
        ScanParams scanParams=new ScanParams().count(100);
        String cursor = "0";
        do{
            ScanResult<Tuple> zscan = jedis.zscan(bigZSetKey, cursor, scanParams);
            List<Tuple> result = zscan.getResult();
            if(result != null && !result.isEmpty()){
                for (Tuple item : result){
                    jedis.zrem(bigZSetKey,item.getElement());
                }
            }
            cursor=zscan.getStringCursor();
        }while (!"0".equals(cursor));

        //最后删除key
        jedis.del(bigZSetKey);
    }

}
